package com.ydjk.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ydjk.domain.JkGroupUser;
import com.ydjk.domain.JkUser;
import com.ydjk.service.IJkGroupUserService;
import com.ydjk.service.IJkUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ydjk.common.annotation.Log;
import com.ydjk.common.core.controller.BaseController;
import com.ydjk.common.core.domain.AjaxResult;
import com.ydjk.common.enums.BusinessType;
import com.ydjk.domain.JkGroup;
import com.ydjk.service.IJkGroupService;
import com.ydjk.common.utils.poi.ExcelUtil;
import com.ydjk.common.core.page.TableDataInfo;

/**
 * 推送组管理Controller
 *
 * @author ydjk
 * @date 2023-08-30
 */
@RestController
@RequestMapping("/push/group")
public class JkGroupController extends BaseController
{
    @Autowired
    private IJkGroupService jkGroupService;

    @Autowired
    private IJkGroupUserService groupUserService;

    @Autowired
    private IJkUserService jkUserService;

    /**
     * 查询推送组管理列表
     */
    @PreAuthorize("@ss.hasPermi('push:group:list')")
    @GetMapping("/list")
    public TableDataInfo list(JkGroup jkGroup)
    {
        startPage();
        List<JkGroup> list = jkGroupService.selectJkGroupList(jkGroup);
        return getDataTable(list);
    }

    /**
     * 导出推送组管理列表
     */
    @PreAuthorize("@ss.hasPermi('push:group:export')")
    @Log(title = "推送组管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JkGroup jkGroup)
    {
        List<JkGroup> list = jkGroupService.selectJkGroupList(jkGroup);
        ExcelUtil<JkGroup> util = new ExcelUtil<JkGroup>(JkGroup.class);
        util.exportExcel(response, list, "推送组管理数据");
    }

    /**
     * 获取推送组管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('push:group:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(jkGroupService.selectJkGroupById(id));
    }

    /**
     * 新增推送组管理
     */
    @PreAuthorize("@ss.hasPermi('push:group:add')")
    @Log(title = "推送组管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JkGroup jkGroup)
    {
        return toAjax(jkGroupService.insertJkGroup(jkGroup));
    }

    /**
     * 修改推送组管理
     */
    @PreAuthorize("@ss.hasPermi('push:group:edit')")
    @Log(title = "推送组管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JkGroup jkGroup)
    {
        return toAjax(jkGroupService.updateJkGroup(jkGroup));
    }

    /**
     * 删除推送组管理
     */
    @PreAuthorize("@ss.hasPermi('push:group:remove')")
    @Log(title = "推送组管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(jkGroupService.deleteJkGroupByIds(ids));
    }

    @GetMapping(value = "/getGroupUser/{id}")
    public AjaxResult getGroupUser(@PathVariable("id") Long id)
    {
        JkGroupUser jkGroupUser = new JkGroupUser();
        jkGroupUser.setGroupId(id);
        List<JkGroupUser> jkGroupUsers = groupUserService.selectJkGroupUserList(jkGroupUser);
        List<JkUser> users = new ArrayList<>();
        if(jkGroupUsers.size()>0){
            ArrayList<Long> userIds = new ArrayList<>();
            jkGroupUsers.forEach(user->{
                userIds.add(user.getUserId());
            });
            users = jkUserService.selectJkUserByIds(userIds);
        }
        return success(users);
    }
}
